package com.moorror.codegenvel.db.sqlite.dao.impl;

import com.google.common.collect.Sets;
import com.moorror.codegenvel.db.sqlite.dao.BaseDAO;
import com.moorror.codegenvel.db.sqlite.dao.CodeGenSceneDao;
import com.moorror.codegenvel.db.sqlite.entity.CodeGenScene;
import com.moorror.codegenvel.db.sqlite.entity.CodeGenSceneTemplate;
import com.moorror.codegenvel.db.sqlite.entity.CodeGenTemplate;
import com.moorror.codegenvel.utils.DateUtils;
import com.moorror.codegenvel.utils.JacksonUtils;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;

import java.sql.Connection;
import java.util.List;
import java.util.Set;

/**
 * @Author jiyanghuang
 * @Date 2025/8/21 07:55
 */
public class CodeGenSceneDaoImpl extends BaseDAO<CodeGenScene> implements CodeGenSceneDao<CodeGenScene> {

    public static final String NAME_TABLE = "codegen_scene";

    private static CodeGenSceneDaoImpl instance;

    private CodeGenSceneDaoImpl() {

    }

    public static synchronized CodeGenSceneDaoImpl getInstance() {
        if (null == instance) {
            instance = new CodeGenSceneDaoImpl();
        }
        return instance;
    }

    private static final String SELECT_SQL = " select id,name,desc_scene,json_context,sql_ddl,create_time,update_time from codegen_scene ";

    @Override
    public String tableName() {
        return "codegen_scene";
    }

    @Override
    public void insert(Connection conn, CodeGenScene[] ary) {
        if (ary == null || ary.length == 0) {
            return;
        }
        //数据库中存在自增列后，SQLite 就会创建一个 sqlite_sequence 表。所有表的自增列都共享这个表，sqlite_sequence 分别维护着每个自增列的当前值，所以自增列的计数也是单独的。
        String sql = "insert into codegen_scene(name,desc_scene,json_context,sql_ddl,create_time,update_time) values(?,?,?,?,?,?)";
        int size = ary.length;
        if (size == 1) {
            CodeGenScene codeGenScene = ary[0];
            update(conn, sql, codeGenScene.getName(), codeGenScene.getDescScene(), codeGenScene.getJsonContext(), codeGenScene.getSqlDdl(), codeGenScene.getCreateTime(), codeGenScene.getUpdateTime());
        } else {
            Object[][] args = new Object[size][5];
            for (int i = 0; i < size; i++) {
                CodeGenScene codeGenScene = ary[i];
                args[i][0] = codeGenScene.getName();
                args[i][1] = codeGenScene.getDescScene();
                args[i][2] = codeGenScene.getJsonContext();
                args[i][3] = codeGenScene.getSqlDdl();
                args[i][4] = codeGenScene.getCreateTime();
                args[i][5] = codeGenScene.getUpdateTime();
            }
            updateBatch(conn, sql, args);
        }
    }

    @Override
    public CodeGenScene insert(Connection conn, CodeGenScene codeGenScene) {
        String sql = "insert into codegen_scene(name,desc_scene,json_context,sql_ddl,create_time,update_time) values(?,?,?,?,?,?)";
        update(conn, sql, codeGenScene.getName(), codeGenScene.getDescScene(), codeGenScene.getJsonContext(), codeGenScene.getSqlDdl(), codeGenScene.getCreateTime(), codeGenScene.getUpdateTime());
        String sqlSelect = SELECT_SQL + " where id = (select LAST_INSERT_ROWID()) limit 1";
        return getBean(conn, sqlSelect);
    }

    @Override
    public void delete(Connection conn, Integer id) {

    }

    @Override
    public boolean deleteByUnique(Connection conn, CodeGenScene updateT) {
        String sql = "delete from codegen_scene where name = ? ";
        return update(conn, sql, updateT.getName()) > 0;
    }

    @Override
    public void update(Connection conn, CodeGenScene[] ary) {

    }

    @Override
    public void update(Connection conn, CodeGenScene codeGenScene) {

    }

    @Override
    public boolean updateByUnique(Connection conn, CodeGenScene updateT) {
        String sql = "update codegen_scene SET desc_scene = ?, json_context = ?, sql_ddl = ?, update_time = ? where name = ? ";
        return update(conn, sql, updateT.getDescScene(), updateT.getJsonContext(), updateT.getSqlDdl(), DateUtils.currentTime(), updateT.getName()) > 0;
    }

    @Override
    public CodeGenScene findById(Connection conn, Integer id) {
        return null;
    }

    @Override
    public CodeGenScene findByUnique(Connection conn, CodeGenScene queryT) {
        String sql = SELECT_SQL + " where name = ? ";
        List<CodeGenScene> re = queryList(conn, sql, queryT.getName());
        if (CollectionUtils.isEmpty(re)) {
            return null;
        }
        return re.get(0);
    }

    @Override
    public List<CodeGenScene> searchLike(Connection conn, String searchValue) {
        searchValue = searchValue + "%";
        String sql = SELECT_SQL + "where name Like ? OR desc_scene Like ? LIMIT 20";
        List<CodeGenScene> re = queryList(conn, sql, searchValue, searchValue);
        if(CollectionUtils.isEmpty(re)){
            return re;
        }
        Set<String> scene = Sets.newHashSet();
        for(CodeGenScene codeGenScene : re){
            scene.add(codeGenScene.getName());
        }

        return re;
    }

    @Override
    public List<CodeGenScene> searchLikeMTM(String searchValue) {
        return manyToMany(searchValue,
                CodeGenSceneDaoImpl.getInstance(),
                CodeGenScene::getName,
                CodeGenSceneTemplateDaoImpl.getInstance(),
                CodeGenSceneTemplate::getTemplate,
                CodeGenSceneTemplate::getScene,
                CodeGenTemplateDaoImpl.getInstance(),
                CodeGenTemplate::getName,
                CodeGenSceneDaoImpl::convertFill);
    }

    @Override
    public void exchangeShowOrder(Connection conn, Integer showOrder1, Integer showOrder2) {

    }

    public static void convertFill(CodeGenSceneTemplate codeGenSceneTemplate, CodeGenTemplate codeGenTemplate) {
        if (null == codeGenSceneTemplate || null == codeGenSceneTemplate) {
            return;
        }
        if (StringUtils.isNotEmpty(codeGenSceneTemplate.getJsonSceneTemplate())) {
            CodeGenTemplate codeGenTemplateJsonFrom = JacksonUtils.toJavaObject(codeGenSceneTemplate.getJsonSceneTemplate(), CodeGenTemplate.class);
            codeGenTemplate.fillJsonValue(codeGenTemplateJsonFrom);
        }
    }
}
